﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipSys.Common.Library;
using VipSys.DataLayer;
using VipSys.SystemEntity;
using System.Text;
namespace VipSys
{
    public partial class MainForm : System.Web.UI.Page
    {
        private List<SysMenu> lstMenus = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            SysAccount sysAccount = Session["SysAccount"] as SysAccount;
            if (sysAccount==null)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('登录超时，请重新登录!')</script>");
                Response.Redirect("Login.aspx");
                return;
            }
            string action =Request["action"];
            if (!string.IsNullOrEmpty(action))
            {
                switch (action)
                {
                    case "Test":
                        Test();
                        return;
                    case "TreeMenu":
                        TreeMenu();
                        return;
                    default:
                        return;
                }
            }
        }
        private void Test()
        {
            string key1 = Request["Key1"];
            Response.Write("({sitename:'dreamdu'})");
            Response.End();
        }
        private void TreeMenu()
        {
            SysAccount sysAccount = Session["SysAccount"] as SysAccount;
            var ParentID=Request["parentid"];
            if (this.lstMenus == null)
                this.lstMenus = new List<SysMenu>();
            MenuAccess.Instance.GetMenus(ref lstMenus);
            StringBuilder jsons = new StringBuilder();
            try
            {
                jsons.Append("[");
                foreach (var Menu in lstMenus)
                {
                    if (Menu.Role.IndexOf(sysAccount.RoleType) < 0)
                        continue;
                    
                        jsons.Append("{");
                        jsons.Append(string.Format(" text: \"{0}\" ,qtip: \"{1}\",id:\"{2}\",iconCls:\"{3}\"", Menu.Text, Menu.Qtip, Menu.ID, Menu.IconCls));
                        jsons.Append(" ,leaf: true ");
                        jsons.Append("},");
                    
                }
                if (jsons.ToString().Substring(jsons.Length - 1, 1) == ",")
                {
                    jsons.Remove(jsons.Length - 1, 1);
                }
                jsons.Append("]");
            }
            catch (Exception)
            {
                jsons.Remove(0,jsons.Length);
                jsons.Append(@"{success:false}");
            }
            this.Response.Clear();
            this.Response.Write(jsons.ToString());
            this.Response.End();

        }
    }
}